package com.csun.cmny.provider.controller;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.dto.BuildingSortSaveDto;
import com.csun.cmny.provider.model.vo.BuildingSortVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcBuildingSortService;
import com.csun.cmny.util.PubUtils;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.google.common.base.Preconditions;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-03-11 11:06
 * @Description: the class omc building sort controller
 */
@RestController
@RequestMapping(value = "/building", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcBuildingSortController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcBuildingSortService omcBuildingSortService;

    /**
     * 查询楼栋排序.
     *
     * @return the wrapper
     */
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/queryBuildingSort")
    public Wrapper queryBuildingSort() {

        BuildingSortVo buildingSortVo = omcBuildingSortService.queryBuildingSort(getLoginAuthDto().getGroupId());

//        return WrapMapper.ok(buildingSortVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), buildingSortVo);
    }

    /**
     * 保存楼栋排序
     *
     * @param buildingSort  building sort
     *
     * @return the wrapper
     */
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/saveBuildingSort")
    public Wrapper saveBuildingSort(@RequestParam("buildingSort") String buildingSort) {

        Preconditions.checkArgument(!PubUtils.isNull(buildingSort),
                cmnyMessageSource.getMessage("building.sort.null", null));

        BuildingSortSaveDto buildingSortSaveDto = new BuildingSortSaveDto();
        buildingSortSaveDto.setGroupId(getLoginAuthDto().getGroupId());
        buildingSortSaveDto.setBuildingSort(buildingSort);

        Integer result = omcBuildingSortService.saveBuildingSort(buildingSortSaveDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

//        return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

}
